package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService_lzl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user_lzl")
public class UserController_lzl {
    @Reference
    private UserService_lzl userService_lzl;
    @RequestMapping("/getUsername")
    public Result getUsername(){
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            return new Result(true,MessageConstant.GET_USERNAME_SUCCESS,username);
        } catch (Exception e) {
            return new Result(false,MessageConstant.GET_USERNAME_FAIL);
        }
    }

    /*
     * 分页查询
     * */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService_lzl.findPage(queryPageBean);
        return pageResult;
    }

    /*
     * 添加
     * */
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] ids){
        try {
            userService_lzl.add(ids,user);
            return new Result(true,"添加用户成功！");
        } catch (Exception e) {
            return new Result(false,"添加用户失败！");
        }
    }
    /*
     * 根据id， 查询user，并回显数据
     * */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            com.itheima.pojo.User user = userService_lzl.findById(id);
            return new Result(true,"查询用户成功",user);
        } catch (Exception e) {
            return new Result(false,"查询用户失败");
        }
    }


    /*
     * 根据roleids 查询关联表，并返回roleids
     * */
    @RequestMapping("/findroleIdsByuserId")
    public Result findroleIdsByuserId(Integer id){
        try {
            List<Integer> ids = userService_lzl.findroleIdsByuserId(id);
            return new Result(false,"",ids);
        } catch (Exception e) {
            return new Result(false,"");
        }
    }

    /*
     *
     * 1.根据userId删除与之有关的关联表中的Roleid
     * 2.再修改user
     * 3.后将userid和roleid存入中间关联表
     * */
    @RequestMapping("/update")
    public Result update(Integer[] ids,@RequestBody com.itheima.pojo.User user){

        try {
            userService_lzl.update(ids,user);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }
    /*
     * 根据id，删除user对象
     * */
    @RequestMapping("/delete")
    public Result delete(int id){
        try {
            userService_lzl.delete(id);
            return new Result(true,"删除用户成功！");
        } catch (RuntimeException e) {
            return new Result(false,"删除用户失败！");
        }
    }
    /*
     * 查询所有，进行回显到套餐管理
     * */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<com.itheima.pojo.User> userList = userService_lzl.findAll();
            return new  Result(true,"",userList);
        } catch (Exception e) {
            return new  Result(false,"查询所有用户失败！");
        }
    }

}


